package cn.abyss.admin.ums.web;

import cn.abyss.admin.common.api.CommonResult;
import cn.abyss.admin.ums.business.UmsAdminBusiness;
import cn.abyss.admin.log.BaseLogger;
import cn.abyss.admin.ums.model.dto.MemberRequestDTO;
import cn.abyss.admin.response.AbyResponse;
import cn.abyss.admin.ums.model.param.UmsAdminParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.security.Principal;
import java.util.HashMap;
import java.util.Map;

/**
 * Description:
 *
 * @author zhangsj
 * @date Created on 2021/8/18
 */
@RestController
@RequestMapping("/admin")
public class UmsAdminController {

    @Autowired
    private UmsAdminBusiness umsAdminBusiness;

    /**
     * 注册
     * @param request
     * @return
     */
    @RequestMapping(value="/register", method= RequestMethod.POST)
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public CommonResult register(HttpServletRequest request, @RequestBody UmsAdminParam umsAdminParam) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            umsAdminBusiness.register(umsAdminParam);
            return CommonResult.success(resultMap);
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            BaseLogger.error(e.getMessage(), e);
            return CommonResult.failed(e.getMessage());
        }
    }

    /**
     * 登录
     * @param request
     * @return
     */
    @RequestMapping(value="/login", method= RequestMethod.POST)
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public CommonResult login(HttpServletRequest request, @RequestBody UmsAdminParam umsAdminParam) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            resultMap = umsAdminBusiness.login(umsAdminParam);
            return CommonResult.success(resultMap);
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            BaseLogger.error(e.getMessage(), e);
            return CommonResult.failed(e.getMessage());
        }
    }

    /**
     * 获取当前登录信息
     * @param principal
     * @return
     */
    @RequestMapping(value="/info", method= RequestMethod.POST)
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public CommonResult info(Principal principal) {
        AbyResponse rsp = new AbyResponse();
        Map<String, Object> resultMap = new HashMap<>();
        try {
            resultMap = umsAdminBusiness.getAdminInfo(principal);
            return CommonResult.success(resultMap);
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            BaseLogger.error(e.getMessage(), e);
            return CommonResult.failed(e.getMessage());
        }
    }

    /**
     * 获取验证码
     * @param request
     * @return
     */
    @RequestMapping(value="/getAuthCode", method= RequestMethod.POST)
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public AbyResponse getAuthCode(HttpServletRequest request, @RequestBody MemberRequestDTO requestDTO) {
        AbyResponse rsp = new AbyResponse();
        try {
            Map<String, Object> map = umsAdminBusiness.getAuthCode(requestDTO);
            rsp.success(map);
            return rsp;
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            BaseLogger.error(e.getMessage(), e);
            rsp.failure(e.getMessage());
        }
        return rsp;
    }

    /**
     * 判断验证码是否正确
     * @param request
     * @return
     */
    @RequestMapping(value="/verifyAuthCode", method= RequestMethod.POST)
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public AbyResponse verifyAuthCode(HttpServletRequest request, @RequestBody MemberRequestDTO requestDTO) {
        AbyResponse rsp = new AbyResponse();
        try {
            Map<String, Object> map = umsAdminBusiness.verifyAuthCode(requestDTO);
            rsp.success(map);
            return rsp;
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            BaseLogger.error(e.getMessage(), e);
            rsp.failure(e.getMessage());
        }
        return rsp;
    }

}
